//
//  ValueUtils.m
//  CQRCBank_iPhone
//
//  Created by carlos on 12-12-11.
//  Copyright (c) 2012年 magic-point. All rights reserved.
//

#import "ValueUtils.h"


@implementation ValueUtils
//把金额转换成大写

+(NSString *)getUppercaseBalance:(NSString *)mBalance
{
    double numberals=[mBalance doubleValue];
    NSArray *numberchar = @[@"零",@"壹",@"贰",@"叁",@"肆",@"伍",@"陆",@"柒",@"捌",@"玖"];
    NSArray *inunitchar = @[@"",@"拾",@"佰",@"仟"];
    NSArray *unitname = @[@"",@"万",@"亿",@"万亿"];
    //金额乘以100转换成字符串（去除圆角分数值）
    NSString *valstr=[NSString stringWithFormat:@"%.2f",numberals];
    NSString *prefix;
    NSString *suffix;
    if (valstr.length<=2) {
        prefix=@"零元";
        if (valstr.length==0) {
            suffix=@"零角零分";
        }
        else if (valstr.length==1)
        {
            suffix=[NSString stringWithFormat:@"%@分",[numberchar objectAtIndex:[valstr intValue]]];
        }
        else
        {
            NSString *head=[valstr substringToIndex:1];
            NSString *foot=[valstr substringFromIndex:1];
            suffix=[NSString stringWithFormat:@"%@角%@分",[numberchar objectAtIndex:[head intValue]],[numberchar objectAtIndex:[foot intValue]]];
        }
    }
    else
    {
        prefix=@"";
        suffix=@"";
        int flag=valstr.length-2;
        NSString *head=[valstr substringToIndex:flag-1];
        NSString *foot=[valstr substringFromIndex:flag];
        if (head.length>13) {
            return@"数值太大（最大支持13位整数），无法处理";
        }
        //处理整数部分
        NSMutableArray *ch=[[NSMutableArray alloc]init];
        for (int i = 0; i < head.length; i++) {
            NSString * str=[NSString stringWithFormat:@"%x",[head characterAtIndex:i]-'0'];
            [ch addObject:str];
        }
        int zeronum=0;
        
        for (int i=0; i<ch.count; i++) {
            int index=(ch.count -i-1)%4;//取段内位置
            int indexloc=(ch.count -i-1)/4;//取段位置
            if ([[ch objectAtIndex:i]isEqualToString:@"0"]) {
                zeronum++;
            }
            else
            {
                if (zeronum!=0) {
                    if (index!=3) {
                        prefix=[prefix stringByAppendingString:@"零"];
                    }
                    zeronum=0;
                }
                prefix=[prefix stringByAppendingString:[numberchar objectAtIndex:[[ch objectAtIndex:i]intValue]]];
                prefix=[prefix stringByAppendingString:[inunitchar objectAtIndex:index]];
            }
            if (index ==0 && zeronum<4) {
                prefix=[prefix stringByAppendingString:[unitname objectAtIndex:indexloc]];
            }
        }
        prefix =[prefix stringByAppendingString:@"元"];
        //处理小数位
        if ([foot isEqualToString:@"00"]) {
            suffix =[suffix stringByAppendingString:@"整"];
        }
        else if ([foot hasPrefix:@"0"])
        {
            NSString *footch=[NSString stringWithFormat:@"%x",[foot characterAtIndex:1]-'0'];
            suffix=[NSString stringWithFormat:@"%@分",[numberchar objectAtIndex:[footch intValue] ]];
        }
        else
        {
            NSString *headch=[NSString stringWithFormat:@"%x",[foot characterAtIndex:0]-'0'];
            NSString *footch=[NSString stringWithFormat:@"%x",[foot characterAtIndex:1]-'0'];
            suffix=[NSString stringWithFormat:@"%@角%@分",[numberchar objectAtIndex:[headch intValue]],[numberchar objectAtIndex:[footch intValue]]];
        }
    }
    return [prefix stringByAppendingString:suffix];
}

//把日期转换成字符串     yyyy-MM-dd
+(NSString *)getStringByDate:(NSDate *)mDate
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //设定时间格式,这里可以设置成自己需要的格式
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    return  [dateFormatter stringFromDate:mDate];
}

//把日期转换成字符串     MM-dd
+(NSString *)getStringByDate3:(NSDate *)mDate
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //设定时间格式,这里可以设置成自己需要的格式
    [dateFormatter setDateFormat:@"MM-dd"];
    return  [dateFormatter stringFromDate:mDate];
}

//把日期转换成字符串     yyyyMMdd
+(NSString *)getStringByDate2:(NSDate *)mDate
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //设定时间格式,这里可以设置成自己需要的格式
    [dateFormatter setDateFormat:@"yyyyMMdd"];
    return  [dateFormatter stringFromDate:mDate];
}
//把日期转换成字符串     yyyy-MM
+(NSString *)getStringByDate1:(NSDate *)mDate
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //设定时间格式,这里可以设置成自己需要的格式
    [dateFormatter setDateFormat:@"yyyy-MM"];
    return  [dateFormatter stringFromDate:mDate];
}
//将yyyyMMdd转换成yyyy年MM月dd日
+(NSString *)getStringByDateString:(NSString *)dateString{
    
    NSString *year = [dateString substringWithRange:NSMakeRange(0, 4)];
    NSString *month = [dateString substringWithRange:NSMakeRange(4, 2)];
    NSString *day = [dateString substringWithRange:NSMakeRange(6, 2)];

    return [NSString stringWithFormat:@"%@年%d月%d日",year,[month intValue],[day intValue]];
}
//将yyyyMMdd转换成yyyy.MM.dd
+(NSString *)getStringFromDateString:(NSString *)dateString{
    
    NSString *year = [dateString substringWithRange:NSMakeRange(0, 4)];
    NSString *month = [dateString substringWithRange:NSMakeRange(4, 2)];
    NSString *day = [dateString substringWithRange:NSMakeRange(6, 2)];
    
    return [NSString stringWithFormat:@"%@.%@.%@",year,month,day];
}
//前1个月的日期
+(NSDate *)previousMonth:(NSDate *)date
{
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDateComponents *dc = [cal components:NSYearCalendarUnit | NSMonthCalendarUnit |NSDayCalendarUnit fromDate:date];
    if(dc.month == 1) {
        [dc setYear:dc.year - 1];
        [dc setMonth:12];
    }
    else {
        [dc setMonth:dc.month - 1];
    }
    return [cal dateFromComponents:dc];
}
//后1个月的日期
+(NSDate *)nextMonth:(NSDate *)date
{
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDateComponents *dc = [cal components:NSYearCalendarUnit | NSMonthCalendarUnit |NSDayCalendarUnit fromDate:date];
    if(dc.month == 12) {
        [dc setYear:dc.year + 1];
        [dc setMonth:1];
    }
    else {
        [dc setMonth:dc.month + 1];
    }
    return [cal dateFromComponents:dc];
}

//前3个月的日期
+(NSDate *)threeMonthsAgo:(NSDate *)date
{
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDateComponents *dc = [cal components:NSYearCalendarUnit | NSMonthCalendarUnit |NSDayCalendarUnit fromDate:date];
    if(dc.month == 1) {
        [dc setYear:dc.year - 1];
        [dc setMonth:10];
    }
    else if(dc.month == 2) {
        [dc setYear:dc.year - 1];
        [dc setMonth:11];
    }
    else if(dc.month == 3) {
        [dc setYear:dc.year - 1];
        [dc setMonth:12];
    }
    else {
        [dc setMonth:dc.month - 3];
    }
    return [cal dateFromComponents:dc];
}

+(NSDate *)afterThreeMonths:(NSDate *)date
{
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDateComponents *dc = [cal components:NSYearCalendarUnit | NSMonthCalendarUnit |NSDayCalendarUnit fromDate:date];
    if(dc.month == 10) {
        [dc setYear:dc.year + 1];
        [dc setMonth:1];
    }
    else if(dc.month == 11) {
        [dc setYear:dc.year + 1];
        [dc setMonth:2];
    }
    else if(dc.month == 12) {
        [dc setYear:dc.year + 1];
        [dc setMonth:3];
    }
    else {
        [dc setMonth:dc.month + 3];
    }
    return [cal dateFromComponents:dc];

}
//格式化日期字符串  yyyyMMdd => yyyy-MM-dd
+(NSString *)formatDateString0:(NSString *)dateStr
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyyMMdd"];
    return [self getStringByDate:[dateFormatter dateFromString:dateStr]];
}
//格式化日期字符串  yyyy-MM-dd => yyyyMMdd
+(NSString *)formatDateString2:(NSString *)dateStr
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    return [self getStringByDate2:[dateFormatter dateFromString:dateStr]];
}
//格式化日期字符串  yyyyMM => yyyy-MM
+(NSString *)formatDateString1:(NSString *)dateStr
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyyMM"];
    return [self getStringByDate1:[dateFormatter dateFromString:dateStr]];
}

+(NSString* )formatDateString3:(NSString* )dateStr
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    return [self getStringByDate:[dateFormatter dateFromString:dateStr]];
}

+(NSString* )formatDateString4:(NSString* )dateStr
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    return [self getStringByDate3:[dateFormatter dateFromString:dateStr]];
    

}
//格式化日期字符串  HHmmss => HH:mm:ss
+(NSString *)formatDateString:(NSString *)dateStr from:(NSString *)aformat to:(NSString *)bformat
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:aformat];
    NSDate *temp = [dateFormatter dateFromString:dateStr];
    [dateFormatter setDateFormat:bformat];
    return [dateFormatter stringFromDate:temp];
}

//格式化时间字符串     HHmmss => HH:mm:ss
+(NSString *)formatDateString:(NSString *)dateStr
{
    return [self formatDateString:dateStr from:@"HHmmss" to:@"HH:mm:ss"];
}

/*-------本地推送用------*/
//将String，格式yyyyMMdd,转化成date
+(NSDate *)formatWithString:(NSString *)dateString{
    
    if (!dateString || dateString.length != 8){
        return nil;
    }

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyyMMdd"];
    [formatter setLocale:[NSLocale currentLocale]];
    [formatter setTimeZone: [NSTimeZone systemTimeZone]];
    
    return [formatter dateFromString:dateString];
}
//前2天的晚上7点
+(NSDate *)twoDaysAgo:(NSDate *)date{
    
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDateComponents *dc = [cal components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit fromDate:date];
    dc.hour = 9;
    NSDate *sevenHour = [cal dateFromComponents:dc];
    
    return [NSDate dateWithTimeInterval:-2*24*3600 sinceDate:sevenHour];
}
//前1天得晚上7点
+(NSDate *)previousDay:(NSDate *)date{
    
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDateComponents *dc = [cal components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit fromDate:date];
    dc.hour = 9;
    NSDate *sevenHour = [cal dateFromComponents:dc];
    
    return [NSDate dateWithTimeInterval:-1*24*3600 sinceDate:sevenHour];

}
//当天早上9点
+(NSDate *)currentDate:(NSDate *)date{
    
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDateComponents *dc = [cal components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit fromDate:date];
    dc.hour = 9;

    return [cal dateFromComponents:dc];
}
//明天晚上7点
+(NSDate *)nextDay:(NSDate *)date{
    
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDateComponents *dc = [cal components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit fromDate:date];
    dc.hour = 9;
    NSDate *sevenHour = [cal dateFromComponents:dc];
    
    return [NSDate dateWithTimeInterval:1*24*3600 sinceDate:sevenHour];
}
//后天晚上7点
+(NSDate *)afterTwoDays:(NSDate *)date{
    
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDateComponents *dc = [cal components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit fromDate:date];
    dc.hour = 9;
    NSDate *sevenHour = [cal dateFromComponents:dc];
    
    return [NSDate dateWithTimeInterval:2*24*3600 sinceDate:sevenHour];

}
//大后天晚上7点
+(NSDate *)afterThreeDays:(NSDate *)date{
    
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDateComponents *dc = [cal components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit fromDate:date];
    dc.hour = 9;
    NSDate *sevenHour = [cal dateFromComponents:dc];
    
    return [NSDate dateWithTimeInterval:3*24*3600 sinceDate:sevenHour];

}
//后7天晚上7点
+(NSDate *)afterSevenDays:(NSDate *)date{
    
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDateComponents *dc = [cal components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit fromDate:date];
    dc.hour = 9;
    NSDate *sevenHour = [cal dateFromComponents:dc];
    
    return [NSDate dateWithTimeInterval:7*24*3600 sinceDate:sevenHour];

}
//两个时间的间隔
+(int)daysForTimeInterval:(NSDate *)oneDate with:(NSDate *)twoDate{
    
    NSTimeInterval timeInterval = [oneDate timeIntervalSinceDate:twoDate];
    
    return timeInterval/(3600*24);
}

/*---------end---------*/

//验证date1是大于date2，date1--yyyyMMDD  date2--yyyyMMDD
+(BOOL)date1IsAfterDate2:(NSString *)date1 date2:(NSString *)date2{
    
    //NSDate *cuDate1=[self formatStrToDate:date1];
    //NSDate *cuDate2=[self formatStrToDate:date2];
    
    //BOOL isAfter=[cuDate1 earlierDate:cuDate2]==cuDate2;
    BOOL isAfter=[date1 compare:date2]>0;

    return isAfter;
    
}

//url转码
+(NSString *)getUrlStringFromString:(NSString *)aStr{
    CFStringRef ref=CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
                                            (CFStringRef)aStr,
                                            NULL,
                                            (CFStringRef)@"!*'();:@&=+$,/?%#[]",
                                            kCFStringEncodingUTF8);
    //1NSString  *str = [[NSString  alloc]initWithCString:CFStringGetCStringPtr(ref, 0)
   //                                                     encoding:NSUTF8StringEncoding];
    
    NSString* str1 = [NSString stringWithCString:CFStringGetCStringPtr(ref, 0) encoding:NSUTF8StringEncoding];
    if(IsNilString(str1))
       return @"";
    return str1;
}
#pragma mark 根据身份证号码判断是否成年
+ (BOOL)isAdultWithBirthday:(NSString *)identityCard currentDate:(NSString *)currentDate;
{
    
//    NSString *str = [identityCard substringWithRange:NSMakeRange(6, 8)];
//    
//    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
//    [formatter setDateFormat:@"yyyyMMdd"];
//    NSDate *birthDate = [formatter dateFromString:str];
//    
//    NSCalendar *cal = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
//    NSDateComponents *dc = [cal components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit |NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:birthDate];
//    dc.year+=18;
//    
//    NSDate *myDate= [cal dateFromComponents:dc];//成年后的当天
//    
//    //当前时间
//    NSDate *mDate =  mDate = [NSDate date];;
//    if (IsNilString(currentDate)) {
//        
//        if (IsNilString([DataManager sharedDataManager].currentDate)) {
//            
//            mDate = [NSDate date];
//        }else{
//            
//            mDate = [ValueUtils formatWithString:[[DataManager sharedDataManager].currentDate substringToIndex:8]];
//        }
//        
//    }else{
//        
//        mDate = [ValueUtils formatWithString:currentDate];
//    }
//    
//    if ([myDate earlierDate:mDate] == myDate || [myDate isEqualToDate:mDate] ) {
//        return YES;
//    }
    return NO;
}

+(NSDate *)formatStrToDate:(NSString *)date{
   NSString *year=[date substringWithRange:NSMakeRange(0, 4)];
   NSString *month=[date substringWithRange:NSMakeRange(4, 2)];
   NSString *day=[date substringWithRange:NSMakeRange(6, 2)];
    
   NSDateComponents *comp = [[NSDateComponents alloc]init];
   [comp setMonth:month];
   [comp setDay:day];
   [comp setYear:year];
   NSCalendar *myCal = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
    
   NSDate *myDate= [myCal dateFromComponents:comp];
  
   return myDate;
    
}


#pragma mark - // 当前日期 格式：yyyyMMdd
+ (NSString *)currentDayStringFormatyyyyMMdd {
    NSDate *date = [NSDate date];
    return [ValueUtils formatDateStringToyyyyMMdd:date];
}

+ (NSString *)formatDateStringToyyyyMMdd:(NSDate *)date {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //设定时间格式,这里可以设置成自己需要的格式
    [dateFormatter setDateFormat:@"yyyyMMdd"];
    return  [dateFormatter stringFromDate:date];
}

// 生成两位随机数
static int staticDecadeNumber = 0;
+ (NSString *)doubleDigitRandomNumbers {
    int i = arc4random()%10;
    
    
    if (staticDecadeNumber == 10) {
        staticDecadeNumber = 0;
    }
    NSString *string = [NSString stringWithFormat:@"%i%i",staticDecadeNumber,i];
    
//    int result = staticDecadeNumber*10 + i;
    
    staticDecadeNumber += 1;
    
    return string;
}

+ (NSString *)showSMSAuthCodeSequenceNumString:(NSString *)seqenceNum andString:(NSString *)string{
    if ([string hasSuffix:@":"] || [string hasSuffix:@"："]) {
        string = [string substringToIndex:string.length-1];
    }
    
    NSString *stringAll = [NSString stringWithFormat:@"请输入序号为%@的%@",seqenceNum,string];
    return stringAll;
}


+(NSString *)fillterEight:(NSString *)phoneNumber{
    /*
    NSMutableString *str=[NSMutableString stringWithString:phoneNumber];
    
    NSString * rege = @"[0-9]*$";//*代表无限长度
    NSPredicate * preTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", rege];

    NSRange jiaRange0=[str rangeOfString:@" "];
    if(jiaRange0.location!=NSNotFound){
        [str deleteCharactersInRange:jiaRange0];
    }
    
    NSRange jiaRange1=[str rangeOfString:@","];
    if(jiaRange1.location!=NSNotFound){
        [str deleteCharactersInRange:jiaRange1];
    }
    
    NSRange jiaRange2=[str rangeOfString:@";"];
    if(jiaRange2.location!=NSNotFound){
        [str deleteCharactersInRange:jiaRange2];
    }
    
    NSRange jiaRange3=[str rangeOfString:@"*"];
    if(jiaRange3.location!=NSNotFound){
        [str deleteCharactersInRange:jiaRange3];
    }
    
    NSRange jiaRange4=[str rangeOfString:@"#"];
    if(jiaRange4.location!=NSNotFound){
        [str deleteCharactersInRange:jiaRange4];
    }
    
    NSRange jiaRange=[str rangeOfString:@"+"];
    if(jiaRange.location!=NSNotFound){
        [str deleteCharactersInRange:jiaRange];
    }
    
    NSRange eightRange=[str rangeOfString:@"86"];
    if(eightRange.location==0){
        [str deleteCharactersInRange:eightRange];
    }
    
    NSRange eightRangeO=[str rangeOfString:@"086"];
    if(eightRangeO.location==0){
        [str deleteCharactersInRange:eightRangeO];
    }

    if (![preTest evaluateWithObject:str]) {
       str = (NSMutableString*)[ValueUtils fillterEight:str];
    }
     */
    phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:@" " withString:@""];
    phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:@" " withString:@""];
    phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:@"," withString:@""];
    phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:@";" withString:@""];
    phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:@"*" withString:@""];
    phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:@"#" withString:@""];
    phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:@"+" withString:@""];
    NSMutableString *number = [NSMutableString stringWithString:phoneNumber];
    NSRange eightRange=[number rangeOfString:@"86"];
    if(eightRange.location==0){
        [number deleteCharactersInRange:eightRange];
    }
    
    NSRange eightRangeO=[number rangeOfString:@"086"];
    if(eightRangeO.location==0){
        [number deleteCharactersInRange:eightRangeO];
    }
    
    return number;
    
}

+ (NSString *)clientVersion
{
    NSDictionary* infoDict =[[NSBundle mainBundle] infoDictionary];
    NSString *versionNum = [infoDict objectForKey:@"CFBundleShortVersionString"];
    return versionNum;
}

+ (BOOL)isLeapYear:(int)year
{
    return (year%4==0&&year%100!=0)||(year%100==0&&year%400==0);
}

//将字符串首尾的空格和换行去掉
+(NSString *)trim:(NSString *)str
{
    NSString * retString = nil;
    if (str) {
        NSString *temptext = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
        retString = [temptext stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    }else{
        retString = @"";
    }
    return retString;
}

#pragma mark - // 给数字 加 千分符号,保留两位 小数。
+ (NSString *)insertMicrometerSymbolToMoney:(NSString *)money {
    // 先判断money  中间 是否有 “,”。
    NSRange range1 = [money rangeOfString:@","];
    if (range1.location != NSNotFound) {
        return money;
    }
    
    NSString *mMoney = [NSString stringWithFormat:@"%.2f",money.doubleValue];
    
    // 12345.678   12345723
    NSRange range = NSMakeRange(0, mMoney.length);
    
    // 取整数部分。
    NSRange digitalRange = [mMoney rangeOfString:@"."];
    
    if (digitalRange.location != NSNotFound) {
        range.length = digitalRange.location;
    }
    NSString *subString1 = [mMoney substringFromIndex:digitalRange.location];
    
    NSString *subString = [mMoney substringWithRange:range];
    
    NSMutableString *result = [[NSMutableString alloc] initWithString:subString];
    for (int i = 1; i < subString.length ; i++) {
        if (i%3 == 0) {
            if (subString.length-i != 0) {
                [result insertString:@"," atIndex:subString.length-i];
            }
        }
    }
    return [NSString stringWithFormat:@"%@%@",result,subString1];
}
#pragma mark - 格式化为以万为单位
+(NSString*) formatAmountByWan:(NSString*) amount
{
    NSString* r = @"";
    float a = [amount floatValue];
    float fA = a / 10000;
    
    r = [NSString stringWithFormat:@"%0.2f万", fA];
    
    return r;
}

#pragma mark - 三基色
+(UIColor *) stringTOColor:(NSString *)str
{
    if (!str || [str isEqualToString:@""]) {
        return nil;
    }
    unsigned red,green,blue;
    NSRange range;
    range.length = 2;
    range.location = 1;
    [[NSScanner scannerWithString:[str substringWithRange:range]] scanHexInt:&red];
    range.location = 3;
    [[NSScanner scannerWithString:[str substringWithRange:range]] scanHexInt:&green];
    range.location = 5;
    [[NSScanner scannerWithString:[str substringWithRange:range]] scanHexInt:&blue];
    UIColor *color= [UIColor colorWithRed:red/255.0f green:green/255.0f blue:blue/255.0f alpha:1];
    return color;
}
#pragma mark - 判断贷款金额是不是正确
+(BOOL)isRightAmountFormat:(NSNumber*)money{

    BOOL rb;
    if (money.intValue<10000||money.intValue>300000||money.intValue%1000!=0) {
        
        rb = NO;
        
    }else if(money.intValue>=10000||money.intValue<=300000||money.intValue%1000==0){
        rb = YES;
    }
    return rb;
}
+ (NSString*)whatTypeImage:(NSString *)imageName{
    
    NSString * imageType = nil;
    
    if (IsNilString(imageName)) {
        return nil;
    }
    
    NSArray  * array= [imageName componentsSeparatedByString:@"_"];
    if (array.count>0) {
    
        imageType = (NSString*)array[2];
    }

    return imageType;
    
}
+(NSString *)generateUUID {
    CFUUIDRef uuid;
    CFStringRef uuidStr;
    NSString *result;
    
    uuid = CFUUIDCreate(NULL);
    assert(uuid != NULL);
    
    uuidStr = CFUUIDCreateString(NULL, uuid);
    assert(uuidStr != NULL);
    
    result = [NSString stringWithFormat:@"%@", uuidStr];
    
    CFRelease(uuidStr);
    CFRelease(uuid);
    
    return result;
}

+ (NSString *)intervalSinceNow: (NSString *) theDate
{
    NSString *timeString=@"";
    
    NSDateFormatter *format=[[NSDateFormatter alloc] init];
    [format setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *fromdate=[format dateFromString:theDate];
    NSTimeZone *fromzone = [NSTimeZone systemTimeZone];
    NSInteger frominterval = [fromzone secondsFromGMTForDate: fromdate];
    NSDate *fromDate = [fromdate  dateByAddingTimeInterval: frominterval];
    
    //获取当前时间
    NSDate *adate = [NSDate date];
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSInteger interval = [zone secondsFromGMTForDate: adate];
    NSDate *localeDate = [adate  dateByAddingTimeInterval: interval];
    
    double intervalTime = [fromDate timeIntervalSinceReferenceDate] - [localeDate timeIntervalSinceReferenceDate];
    long lTime = fabs((long)intervalTime);
    NSInteger iSeconds =  lTime % 60;
    NSInteger iMinutes = (lTime / 60) % 60;
    NSInteger iDays = lTime/60/60/24;
    NSInteger iHours = fabs(lTime/3600)-iDays*24;
    
    NSInteger iMonth =lTime/60/60/24/12;
    NSInteger iYears = lTime/60/60/24/384;
    
    ILog(@"")

    

    
    
    NSLog(@"距离截止时间%d日%d时%d分%d秒", iDays,iHours,iMinutes,iSeconds);
    timeString = [NSString stringWithFormat:@"距截止%d日%d时%d分%d秒", iDays,iHours,iMinutes,iSeconds];
 
    return timeString;
}

+ (NSString*) formatDate:(NSString*) orgStrDate InputFormat:(NSString*) inputFormat OutputFormat:(NSString*) outputFormat
{
    //orgStrDate = @"2012/08/01 14:49:10";
    NSString* returnStr = @"";
    NSString* inFormat = inputFormat;
    NSString* outFormat = outputFormat;
    if([inFormat isEqualToString:@""])
    {
        inFormat = @"yyyy/MM/dd HH:mm:ss";
    }
    if([outFormat isEqualToString:@""])
    {
        outFormat = @"yyyy-MM-dd HH:mm";
    }
    
    NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
    [inputFormatter setDateFormat:inFormat];
    NSDate* inputDate = [inputFormatter dateFromString:orgStrDate];
    
    NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
    [outputFormatter setDateFormat:outFormat];
    NSString* strOutputDate = [outputFormatter stringFromDate:inputDate];
    NSDate* outputDate = [outputFormatter dateFromString: strOutputDate];
    
    //当前时间
    NSDate* nowDate = [NSDate date];
    NSDateFormatter* nowYMD = [[NSDateFormatter alloc] init];
    [nowYMD setDateFormat:@"yyyy-MM-dd"];
    NSString* strToday_YMD = [nowYMD stringFromDate:nowDate];
    NSString* strToday_YMDHMS = [NSString stringWithFormat:@"%@ 00:00:00", strToday_YMD];
    
    
    
    //今天标准时间
    NSDateFormatter* todayFormatter = [[NSDateFormatter alloc] init];
    [todayFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    [todayFormatter setLocale:[NSLocale currentLocale]];
    NSDate* todayDate = [todayFormatter dateFromString:strToday_YMDHMS];
    
    //昨天标准时间
    NSDate* yestodayDate = [NSDate dateWithTimeInterval:-60*60*24 sinceDate:todayDate];
    //7天前标准时间
    NSDate* sevenDate = [NSDate dateWithTimeInterval:-7*60*60*24 sinceDate:todayDate];
    
    //上个月标准时间
    NSDate* lastMothDate = [NSDate dateWithTimeInterval:-30*60*60*24 sinceDate:todayDate];
    
    //
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit |NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
    NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:nowDate];
    //上个月标准时间
    int nowYear = [dateComponent year]-1;
    //今年标准时间
    NSDate* nowYearDate = [todayFormatter dateFromString: [NSString stringWithFormat: @"%d-12-31 23:59:59", nowYear]];
    
    // NSString* strLastMoth_YMDHMS =
    //当前时间与传进来的参数时间的差
    NSTimeInterval now_orgTime = [nowDate timeIntervalSinceDate:inputDate];
    //当前时间与今天时间的差
    NSTimeInterval now_todayTime = [nowDate timeIntervalSinceDate:todayDate];
    //当前时间与上个月标准时间的差
    NSTimeInterval now_lastMothTime = [nowDate timeIntervalSinceDate:lastMothDate];
    //当前时间与上年标准时间的差
    NSTimeInterval now_lastYearTime = [nowDate timeIntervalSinceDate:nowYearDate];
    /*
    if(now_orgTime <= 0)
    {
        returnStr = [NSString stringWithFormat:@"刚刚"];
    }
    else if(now_orgTime < 30)
    {
        returnStr = [NSString stringWithFormat:@"%d 秒以前", (int)now_orgTime];
    }
    else if(now_orgTime >= 30 && now_orgTime < 60)
    {
        returnStr = [NSString stringWithFormat:@"半分钟前"];
    }
     */
    if(now_orgTime < 60 * 10)
    {
        returnStr = [NSString stringWithFormat:@"1小时以内发布"];
    }
    else if(now_orgTime >= 60 && now_orgTime < 3600 && now_orgTime < now_todayTime)
    {
        double m = now_orgTime / 60;
        //returnStr = [NSString stringWithFormat:@"%d分钟前发布", (int)m];
        returnStr = [NSString stringWithFormat:@"1小时以内发布"];
    }
    else if(now_orgTime >= 3600 && now_orgTime < now_todayTime)
   // else
    {
        int  h = now_orgTime / 3600;
        int  m = ((int)now_orgTime % 3600)/60;
        
        if(m  == 0)
        {
            returnStr = [NSString stringWithFormat:@"%d小时前发布", (int)h];
        }
        else
        {
            //returnStr = [NSString stringWithFormat:@"%d小时%d分前发布", (int)h, m];
            returnStr = [NSString stringWithFormat:@"%d小时前发布", (int)h];
        }
        
        if(h < 3)
        {
            returnStr = [NSString stringWithFormat:@"%d 小时前发布", (int)h];
        }
        else
        {
            NSDateFormatter *inputHmFormatter = [[NSDateFormatter alloc] init];
            [inputHmFormatter setDateFormat:@"HH:mm"];
            NSString *str = [inputHmFormatter stringFromDate:outputDate];
            returnStr = [NSString stringWithFormat:@"今天 %@发布", str];
        }
         
    }   // 86400 = 60 * 60 * 24
    
    else if(now_orgTime >= now_todayTime && now_orgTime < (now_todayTime + 86400))
    {
        NSDateFormatter *inputHmFormatter = [[NSDateFormatter alloc] init];
        [inputHmFormatter setDateFormat:@"HH:mm"];
        NSString *str = [inputHmFormatter stringFromDate:outputDate];
        returnStr = [NSString stringWithFormat:@"昨天 %@发布", str];
    } //604800 = 7 * 60 * 60 * 24
    
    else
    {
        double sevenT = now_orgTime / 86400;
        returnStr = [NSString stringWithFormat:@"%d 天前发布", (int)sevenT];
    }
//    else if(now_orgTime >= 604800 && now_orgTime < now_lastYearTime)
//  {
//        NSDateFormatter *inputHmFormatter = [[NSDateFormatter alloc] init];
//        [inputHmFormatter setDateFormat:@"MM-dd HH:mm"];
//        returnStr = [inputHmFormatter stringFromDate:outputDate];
//    }
//    else if(now_orgTime >= now_lastYearTime)
//    {
//        NSDateFormatter *inputHmFormatter = [[NSDateFormatter alloc] init];
//        [inputHmFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
//        returnStr = [inputHmFormatter stringFromDate:outputDate];
//    }
    
    return returnStr;
}
+ (CGSize) ContentHeight:(NSString*) content ContentSize:(CGSize) contentSize FontSize:(float) fontSize
{
    UIFont *font = [UIFont systemFontOfSize:fontSize];
    
    NSDictionary *attrDictionary = [NSDictionary dictionaryWithObject:font
                                                               forKey:NSFontAttributeName];
    NSAttributedString* attrContent = [[NSAttributedString alloc] initWithString:content attributes:attrDictionary];
    CGRect attrContentSize = [attrContent boundingRectWithSize:contentSize options:NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin context:nil];
    
    return attrContentSize.size;
    
}

@end
